<template>
  <view class="pages">
    <view
      class="absolute right-30rpx top-100rpx bg-[rgba(0,0,0,0.3)] text-28rpx text-#ffffff w-130rpx text-center py-4rpx rd-20rpx z-10"
      @click="closeAd"
      >跳过 {{ countdown }}</view
    >
    <image class="w-full h-full" style="vertical-align: middle" :src="url" @click="jump"></image>
  </view>
</template>

<script setup>
import { getBannerList, getProvinceInfo } from '@/api/index';

const url = ref('');
const bannerJumpLink = ref('');
async function getBanner() {
  const res = await getBannerList({ bannerPosition: 3 });

  if (res.data[0]?.bannerUrl) {
    const arr = JSON.parse(res.data[0].bannerUrl);
    url.value = arr[0].url;
    bannerJumpLink.value = res.data[0]?.bannerJumpLink;

    timer = setInterval(() => {
      if (countdown.value > 1) {
        countdown.value--;
      } else {
        closeAd();
      }
    }, 1000);
  } else {
    closeAd();
  }
}

function jump() {
  if (!bannerJumpLink.value) return;
  // #ifdef APP
  plus.runtime.openURL(bannerJumpLink.value);
  // #endif
}

const countdown = ref(3);
let timer = null;
function closeAd() {
  timer && clearInterval(timer);
  getUserInfo();
}
function getUserInfo() {
  getProvinceInfo().then((res) => {
    if (res.data.pointFlag === 1) {
      uni.reLaunch({
        url: '/pages/index/index'
      });
      return;
    }
    if (res.data.registerInfoOk !== 1) {
      // 未填写信用代码等企业信息
      uni.reLaunch({
        url: '/pages/common/addInformation'
      });
      return;
    }
    if (res.data.everPurchase !== 1) {
      // 未购买过线索包
      uni.reLaunch({
        url: '/pages/common/buyClue?back=0'
      });
      return;
    }
    if (res.data.haveProduct !== 1) {
      // 购买的线索包未绑定一类产品
      uni.reLaunch({
        url: '/pages/common/bindOneProduct?back=0'
      });
      return;
    }
    uni.reLaunch({
      url: '/pages/index/index'
    });
  });
}

onLoad(async () => {
  getBanner();
});
onUnload(() => {
  timer && clearInterval(timer);
});
</script>

<style lang="scss" scoped>
.pages {
  position: relative;
  height: calc(100vh - var(--window-top));
}
</style>
